<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\DB;

class CheckMobile
{

    /**
     * 校验手机号
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (!$request->userInfo->mobile) {
            // 如果抽奖和领取超过四次，则需要绑定手机号
            $drawNum = DB::table('draw_log')->where('user_id', $request->userInfo->id)
                ->where('is_luck', 1)
                ->count('*');
            if ($drawNum >= 4) {
                return response()->json(['message' => '请先绑定手机号', 'success' => 'false'], 420);
            }
            $getNum =  DB::table('user_get_log')->where('user_id', $request->userInfo->id)->count('*');
            if ($getNum >= 4 || $drawNum + $getNum >= 4) {
                return response()->json(['message' => '请先绑定手机号', 'success' => 'false'], 420);
            }
        }
        return $next($request);
    }
}
